#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace KLib.Physics.Collisions
{
    public class BruteForceCollider : IBroadPhaseCollider
    {
        #region Fields
        RigidBody body1;
        RigidBody body2;
        Geom geom1;
        Geom geom2;
        PhysicsManager physics;

        #endregion

        #region Properties

        #endregion

        #region Initialization

        public BruteForceCollider(PhysicsManager physicsManager)
        {
            this.physics = physicsManager;
        }

        #endregion

        #region Methods
        public void Update()
        {
            for (int i = 0; i < physics.Geometries.Count-1; i++)
            {
                for (int k = i+1; k < physics.Geometries.Count; k++)
                {  
                    //Check for possible early exits (A little non-brute force for us :-)
                    geom1 = physics.Geometries[i];
                    geom2 = physics.Geometries[k];

                    body1 = geom1.Body;
                    body2 = geom2.Body;

                    if (body1 == null || body2 == null)
                        continue;
                    if (!body1.Enabled || !body2.Enabled)
                        continue;
                    if (body1.IsStatic && body2.IsStatic)
                        continue;
                    if (body1.Geometry.Equals(body2.Geometry))
                        continue;

                    bool intersects = true;

                    //AABB.Intersects(body1, body2);
                    if (geom1.Bounds.Min.X > geom2.Bounds.Max.X || geom2.Bounds.Min.X > geom1.Bounds.Max.X)
                        intersects = false;
                    else if (geom1.Bounds.Min.Y > geom2.Bounds.Max.Y || geom2.Bounds.Min.Y > geom1.Bounds.Max.Y)
                        intersects = false;

                    intersects = body1.Bounds.Intersects(body2.Bounds);


                    if (intersects)
                    {
                        Arbiter2 arbiter = physics.ArbiterPool.Fetch();
                        arbiter.ConstructArbiter(geom1, geom2, physics);

                        if (!physics.ArbiterList.Contains(arbiter))
                            physics.ArbiterList.Add(arbiter);
                        else
                            physics.ArbiterPool.Release(arbiter);
                    }
                }
            }
        }
        #endregion

        #region Helper Methods

        #endregion
    
        #region IBroadPhaseCollider Members

public void  ProcessAddedGeometry()
{
 	//Not needed for Brute force
}

public void  ProcessRemovedGeometry()
{
 	//Not needed for Brute Force
}

public void  Add(Geom geom)
{
 	//Not needed for Brute Force
}

#endregion
    }   
}
